package displayboardinfo.entities;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 *  
 * Klasa reprezentująca pojedynczy gabinet lekarski.
 *
 * @author Artur Jaworski
 */
@Entity
@Table ( name = "Rooms" )
@XmlRootElement
@XmlType(propOrder = { "id", "name", "number", "users", "visits", "admissions", "registeredForUpdates" })
public class Room {
	@Id
	@Column ( name = "id" )
	@GeneratedValue(strategy=GenerationType.AUTO)
	protected long id;
	
	@Column ( name = "name", nullable = false )
	protected String name;
	
	@Column ( name = "number", nullable = true )
	protected String number;
	
	@ManyToMany
	@JoinTable ( name = "Privileges", joinColumns = {@JoinColumn ( name = "id_room")}, inverseJoinColumns = {@JoinColumn ( name = "id_user" )} )
	protected List<User> users;
	
	@OneToMany ( mappedBy = "room" )
	protected List<Visit> visits;
	
	@OneToMany ( mappedBy = "room" )
	protected List<Admission> admissions;
	
	@OneToMany ( mappedBy = "room" )
	protected List<RegisteredForUpdates> registeredForUpdates;
	
	public Room() {
	
	}
	
	public Room(long id){
		this.id = id;
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}
	
	@XmlElementWrapper ( name = "users" )
	@XmlElement ( name = "user" )
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	@XmlElementWrapper ( name = "visits" )
	@XmlElement ( name = "visit" )
	public List<Visit> getVisits() {
		return visits;
	}

	public void setVisits(List<Visit> visits) {
		this.visits = visits;
	}

	@XmlElementWrapper ( name = "admissions" )
	@XmlElement ( name = "admission" )
	public List<Admission> getAdmissions() {
		return admissions;
	}

	public void setAdmissions(List<Admission> admissions) {
		this.admissions = admissions;
	}

	@XmlElementWrapper ( name = "registeredForUpdates" )
	@XmlElement ( name = "registeredForUpdates" )
	public List<RegisteredForUpdates> getRegisteredForUpdates() {
		return registeredForUpdates;
	}

	public void setRegisteredForUpdates(List<RegisteredForUpdates> registeredForUpdates) {
		this.registeredForUpdates = registeredForUpdates;
	}

	@Override
	public String toString() {
		String text = new String();
		
		text += "\nid:" + id + "\n" +
				"name:" + name + "\n" +
				"number:" + number;
		if (users != null && users.size() > 0){
			text += "\nusers:";
			for (User u : users)
				text += u.getId() + ",";
		}
		if (visits != null && visits.size() > 0){
			text += "\nvisits:";
			for (Visit v : visits)
				text += v.getId() + ",";
		}
		if (admissions != null && admissions.size() > 0){
			text += "\nadmissions:";
			for (Admission a : admissions)
				text += a.getId() + ",";
		}
		if (registeredForUpdates != null && registeredForUpdates.size() > 0){
			text += "\nregisteredForUpdates:";
			for (RegisteredForUpdates r : registeredForUpdates)
				text += r.getId() + ",";
		}
		
		return text;
	}
}
